<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Myinfo extends CI_Controller
{
    public $_food;
    public $_wood;
    public $_metal;
    public $_petroleum;
    public $_NUS;
	function Myinfo()
	{
		parent::__construct();
        $this->_food = $this->config->item('food');
		$this->_wood = $this->config->item('wood');
		$this->_metal = $this->config->item('metal');
		$this->_petroleum = $this->config->item('petroleum');
        $this->_NUS = $this->config->item('NUS');
        $this->load->model('common/common_model');
	}
    
    function loadUserinfo($userid, $user_oasis_orderid)
    {
        $time = time();
        $res = $this->common_model->ww_get_res_user_info_by_field($userid, $user_oasis_orderid);
        $timepass = (((($time - intval($res['lastupdate']))/60)/60)/2);
		$food = floor($timepass*$this->_food);
		$wood = floor($timepass*$this->_wood);
		$metal = floor($timepass*$this->_metal);
		$petroleum = floor($timepass*$this->_petroleum);
        
        $food = ($res['maxstore'] < $res['food'] + $food) ? $res['maxstore'] - $res['food'] : $food;
        $wood = ($res['maxstore'] < $res['wood'] + $wood) ? $res['maxstore'] - $res['wood'] : $wood;
        $metal = ($res['maxstore'] < $res['metal'] + $metal) ? $res['maxstore'] - $res['metal'] : $metal;
        $petroleum = ($res['maxstore'] < $res['petroleum'] + $petroleum) ? $res['maxstore'] - $res['petroleum'] : $petroleum;
        
		if($food >= 1 || $wood >= 1 || $metal >= 1 || $petroleum >= 1)
		{
            $this->common_model->ww_update_res_by_hour($userid, $user_oasis_orderid, $food, $wood, $metal, $petroleum, $time);
		}
        
        $this->update_all_train_by_user($userid, $user_oasis_orderid);
        //$this->_NUS resoure will increase 1s
        $data = array(  'rtf' => (10*$this->_NUS)/($this->_food/60),
                        'rtw' => (10*$this->_NUS)/($this->_wood/60),
                        'rtm' => (10*$this->_NUS)/($this->_metal/60),
                        'rtp' => (10*$this->_NUS)/($this->_petroleum/60)
        );
        $resource = $this->common_model->ww_get_res_user_info_by_field($userid, $user_oasis_orderid);
        $data['res'] = $resource;
        $data['coor'] = $user_oasis_orderid;
        
        return $data;
    }
    
	function update_all_train_by_user($userid, $oasis_orderid)
	{
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        
        $strComp = '';
		$chkTrain = false;
		$arr_u = $arr_v = $arrComp = array();
		$train = $this->common_model->ww_get_train_user_info($userid, $oasis_orderid);
		foreach($train as $lst)
		{
			//delay time 5s.
			$timestamp = $lst['timestamp'] + $lst['eachtime'] + 5;
			$timenow = time();
			
			if($timenow > $timestamp)
			{
				//get id complete
				$arrComp[] = $lst['id'];
				if(!in_array('u'.$lst['unit'], $arr_u))
				{
					$arr_u[] = 'u'.$lst['unit'];
					$arr_v[] = $lst['lquantity'] - $lst['quantity'];
				}
				else
				{
					$valKey = array_search('u'.$lst['unit'], $arr_u);
					$arr_v[$valKey] = $arr_v[$valKey] + $lst['lquantity'] - $lst['quantity'];
				}
				$chkTrain = true;
			}
			else
			{
				//id not complete -> run countdown time
				$chkTime_v1 = $lst['timestamp'] + $lst['eachtime']; // time complete
                
				//time complete training from now
				$chkTime_v2 = $timenow + $lst['fixtime']*$lst['lquantity'];
				
				//remaining time training 
				$chkTime_v3 = $lst['eachtime'] - $lst['fixtime']*$lst['lquantity'];
				
				
				//time 2 > time 1-> training to prepare complete
				//review it
				if($chkTime_v2 > $chkTime_v1)
				{
					//$quantity is primes of training complete
					$decimal = ($timenow - ($lst['timestamp']+$chkTime_v3))/$lst['fixtime'];
					$quantity = floor($decimal);
					$quantity = $quantity > $lst['lquantity'] ? $lst['lquantity'] : $quantity;
                    // check it complete least 1 value then update to db
					if($decimal >= 1 && $quantity>=1 && $quantity > $lst['quantity'])
					{
						$quantity = $quantity - $lst['quantity'];
                        $this->common_model->ww_update_quantity_for_time($lst['id'], $userid, $oasis_orderid, $quantity);
					}
				}
			}//end if
		}//end for
        
        
		if($chkTrain == true)
		{
            $i = 0;
			$arrStr = array();
			$lstid = join(",", $arrComp);
			foreach($arr_u as $tp)
			{
				$strComp .= $tp.'='.$tp.'+'.$arr_v[$i].',';
				$i++;
			}
			$strComp = substr($strComp, 0, -1);
			$this->common_model->ww_update_quantity_military($userid, $oasis_orderid, $strComp, $lstid);
		}
	}
    
    function upgrade_all_build_by_user($userid, $oasis_orderid)
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        
        $strComp = '';
        $chkComp = false;
        $arr_v1 = $arr_v2 = $arrbuild = array();
        $upgrade = $this->common_model->ww_get_user_info_build($userid, $oasis_orderid);
        foreach($upgrade as $lst)
        {
            $timeup = $lst['timestamp'] + $lst['eachtime'];
            $now = time();
            $elaptime = $now - $timeup;
            if($elaptime >= 0)
            {
                // complete
                $arr_v1['id'][] = $lst['id'];
                $arr_v1['buildid'][] = $lst['buildid'];
                $chkComp = true;
            }
            else
            {
                // close complete
                $arr_v2['id'] = $lst['id'];
                $arr_v2['rtime'] = $lst['timestamp'] + $lst['eachtime'] - $now;
                $arrbuild[] = $arr_v2;
            }
        }
        
		if($chkComp == true)
		{
            $i=0;
			$lstid = join(",", $arr_v1['id']);
			foreach($arr_v1['buildid'] as $lst)
			{
				$strComp .= 'b'.$lst.'=b'.$lst.'+1,';
				$i++;
			}
            $strComp = substr($strComp, 0, -1);
			$this->common_model->ww_update_build_by_user($userid, $oasis_orderid, $strComp, $lstid);
		}
        return $arrbuild;
    }
}